//
//  NSDictionary+Safe.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/12/21.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "NSDictionary+Safe.h"

#define isValidKey(key) ((key) != nil && ![key isKindOfClass:[NSNull class]])
#define isValidValue(value) (((value) != nil) && ![value isKindOfClass:[NSNull class]])

@implementation NSDictionary (Safe)


- (id)safeObjectForKey:(id)key{
    if (!isValidKey(key)) {
        return nil;
    }
    id obj = [self objectForKey:key];
    if(!isValidValue(obj))
        return nil;
    return obj;
}

- (int)intValueForKey:(id)key{
    id obj = [self safeObjectForKey:key];
    return [obj intValue];
}

- (double)doubleValueForKey:(id)key{
    id obj = [self safeObjectForKey:key];
    return [obj doubleValue];
}

- (NSString*)stringValueForKey:(id)key{
    id obj = [self safeObjectForKey:key];
    if ([obj respondsToSelector:@selector(stringValue)]) {
        return [obj stringValue];
    }
    return nil;
}

//- (NSString*)unescapeStringForKey:(id)key{
//    NSString *string = [self safeObjectForKey:key];
//    if ([string isKindOfClass:[NSString class]]) {
//        return [string unescapeHTML];
//    }
//    return nil;
//}

@end


@implementation NSMutableDictionary(Safe)

- (void)safeSetObject:(id)anObject forKey:(id)aKey{
    if (!isValidKey(aKey)) {
        return;
    }
    
    if (anObject && ![anObject  isEqual:[NSNull  null]]) {
        if ([aKey  isKindOfClass:[NSString  class]]) {
            [self  setValue:anObject forKey:aKey];
        }else {
            [self setObject:anObject forKey:aKey];
        }
    }else {
        [self removeObjectForKey:aKey];
    }
    
//    if ([aKey isKindOfClass:[NSString class]]) {
//        [self setValue:anObject forKey:aKey];
//    }
//    else{
//        if (anObject != nil) {
//            [self setObject:anObject forKey:aKey];
//        }
//        else{
//            [self removeObjectForKey:aKey];
//        }
//    }
}
- (void)setIntValue:(int)value forKey:(id)aKey{
    [self safeSetObject:[[NSNumber numberWithInt:value] stringValue] forKey:aKey];
}
- (void)setDoubleValue:(double)value forKey:(id)aKey{
    [self safeSetObject:[[NSNumber numberWithDouble:value] stringValue] forKey:aKey];
    
}
- (void)setStringValueForKey:(NSString*)string forKey:(id)aKey{
    [self safeSetObject:string forKey:aKey];
}

+(NSDictionary *)nullDic:(NSDictionary *)myDic
{
    NSArray *keyArr = [myDic allKeys];
    NSMutableDictionary *resDic = [[NSMutableDictionary alloc]init];
    for (int i = 0; i < keyArr.count; i ++)
    {
        id obj = [myDic objectForKey:keyArr[i]];
        
        obj = [self changeType:obj];
        
        [resDic setObject:obj forKey:keyArr[i]];
    }
    return resDic;
}

+(NSArray *)nullArr:(NSArray *)myArr
{
    NSMutableArray *resArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < myArr.count; i ++)
    {
        id obj = myArr[i];
        
        obj = [self changeType:obj];
        
        [resArr addObject:obj];
    }
    return resArr;
}

+(NSString *)stringToString:(NSString *)string
{
    return string;
}

+(NSString *)nullToString
{
    return @"";
}

+(id)changeType:(id)myObj
{
    if ([myObj isKindOfClass:[NSDictionary class]])
    {
        return [self nullDic:myObj];
    }
    else if([myObj isKindOfClass:[NSArray class]])
    {
        return [self nullArr:myObj];
    }
    else if([myObj isKindOfClass:[NSString class]])
    {
        return [self stringToString:myObj];
    }
    else if([myObj isKindOfClass:[NSNull class]])
    {
        return [self nullToString];
    }
    else if([myObj isKindOfClass:[NSNumber class]])
    {
        return [NSString stringWithFormat:@"%@",myObj];
    }
    else
    {
        return myObj;
    }
}

@end


@implementation NSArray(Exception)

- (id)objectForKey:(id)key{
#ifdef DEBUG
    NSAssert(NO, @"NSArray should not call objectForKey, you should check your code!");
    return nil;
#else
    return nil;
#endif
}

@end

